package cn.i7baozh.starter.service.controller;

import cn.i7baozh.starter.service.service.TestService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author : baoqi.zhang
 * @date :   2022/5/6 17:24
 */
@RefreshScope
@Slf4j
@RestController
@RequestMapping("/service/api/test")
public class TestServiceController {

    @Autowired
    private TestService testService;

    @Value("${sleep.second:1}")
    private Integer second;

    @SentinelResource(value = "serviceSayHello")
    @GetMapping("/sayHello")
    public String sayHello(String name) {
        log.info("service sayHello param:{}", name);
        try {
            TimeUnit.SECONDS.sleep(second);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return testService.sayHello(name);
    }

    @PostMapping("/sayHi")
    public String sayHi(String name) {
        log.info("service sayHi param:{}", name);
        return testService.sayHello(name);
    }
}
